<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\home\model;

use think\Db;
use think\Model;

class IndexModel extends Model
{
	//系统参数
//	public function systemInfo(){
//		$info = Db::table('system')->cache(36000)->where("id=1")->find();
//		// dump($info);die;
//		if($info){
//			$res = $info;
//		}else{
//			$res = 0; //无系统信息
//		}
//		return $res;
//	}

	//首页banner
    public function homeBanner()
    {
    	//查幻灯片类
    	$info = Db::name('slide')->field("id,name")->where("name='首页幻灯片' and status=1")->find();
    	//查询幻灯片
    	if($info){
    		$slide_id = $info['id'];
    		$banner = Db::name('slide_item')->where("slide_id='$slide_id'")->select()->toArray();
    		// dump($banner);die;
    		if($banner){
    			$res = $banner;
    		}else{
    			$res = 0; //无banner
    		}
    	}else{
    		$res = 0; //无banner的分类
    	}
    	return $res;
    }

	//首页导航
    public function navInfo()
    {
    	//导航类
    	$navInfo = Db::name('nav')->field("id,name")->cache(300)->select()->toArray();
    	//主导航
    	if($navInfo){
    		foreach($navInfo as $k=>$v){
    			if($v['name'] == '主导航'){
    				$navId = $v['id'];
    			}
    		}
    		$nav = Db::name('nav_menu')->field("name,list_order,icon")->where("nav_id='$navId' and parent_id=1 and status=1")->cache(300)->select()->toArray();
    		// dump($nav);die;
    		$res = $nav;
    	}else{
    		$res = 0; //无banner的分类
    	}
    	return $res;
    }  

    //热点
	public function hotInfo($id)
    {   
        $base = "http://".$_SERVER['HTTP_HOST']."/";
        // $path = cmf_url('portal/Article/index',array('id'=>1));
        // $url = $ip.$path;
        // echo $url;die;
    	$info = Db::name("portal_category")->where("id='$id'")->find();
        // dump($info);die;
    	if($info){
    		//文章分类的id
    		$cate_id = $info['id'];
    		//目前只提供 是否置顶,点击数,点赞数,标题,摘要,缩略图,内容
    		$hotInfo = Db::name("portal_post")->field("id,post_title,post_num,post_like,published_time")->where("cate_id='$cate_id' and post_status=1")->order('published_time desc')->select()->toArray();
            foreach($hotInfo as $k=>$v){
                $path = $base."portal/Article/index/id/".$v['id'].".html";
                $hotInfo[$k]['url'] = $path;
            }
    		// dump($hotInfo);die;
    		if($hotInfo){
    			$res = $hotInfo;
    		}else{
    			$res = array();
    		}
    	}else{
    		$res = array();
    	}
    	return $res;
    }

    //个人各月份明细
    public function portalInfo($id)
    {
    	$info = Db::table('user_money')->where("user_id='$id'")->select()->toArray();
    	if(!$info){
    		$res = array();
    	}else{
    		$res = $info;
    	}
    	return $res;
    }

    //用户
    public function agentMonthMoney($user_id){
        $res = array();
        $month = date("Ym",time());
        //查询当前代理的信息
        $agentWhere = "user_id='$user_id'";
        $agent = Db::table('user_money')->field('month_money,all_profit,add_agent,add_shop')->where($agentWhere)->order("month desc")->select()->toArray();
        // dump($agent);die;
        if($agent){
            if(count($agent) == 1){
                $res = $agent[0];
            }else{        
                $res = $agent[0];
                $res['month_money'] = $agent[0]['month_money'] - $agent[1]['month_money'];
                $res['all_profit'] = $agent[0]['all_profit'] - $agent[1]['all_profit'];
                $res['add_agent'] = $agent[0]['add_agent'] - $agent[1]['add_agent'];
                $res['add_shop'] = $agent[0]['add_shop'] - $agent[1]['add_shop'];
            }
            $res = $res;
        }else{
            Db::table('user_money')->insert($addData=array('user_id'=>$user_id,'month'=>$month));
            $res['month_money'] = 0;
            $res['all_profit'] = 0;
            $res['add_agent'] = 0;
            $res['add_shop'] = 0;
        } 
        return $res;     
    }    

    // 月折线图
    public function allProfit($user_id){
        $year = date('Y');
        $month = date('m');
        // $days=date('t',strtotime("$year-$months-1"));//当前日期
        // $day = date("Ymd",time());
        //封装where条件 并查询该代理商下级代理商的交易信息
        $where = "user_id='$user_id'";
        //月
        $agentInfo = Db::table('user_money')->field('month_teach_profit,month_pos_profit,month_direct_profit,month_train_profit,month,machine_num,month_quick_money,month_normal_money,month_yquick_money,month_quick_profit,month_normal_profit,month_yquick_profit')->where($where)->order('month asc')->select()->toArray();
        $monthInfo = array();
        if($agentInfo){
            //拼接月份的数据
            $num = array();
            for($i=0;$i<12;$i++){
                $num[$i]['profit'] = 0;
                $num[$i]['month'] = $i+1;
            }           
            foreach ($agentInfo as $k => $v) {
                $monthInfo[$k]['profit'] = $v['month_teach_profit'] + $v['month_pos_profit'] + $v['month_direct_profit'] + $v['month_train_profit'];
                // $month[$k]['month'] = $v['month'];
                $monthInfo[$k]['month'] = (int)substr($v['month'],4);
                $num[$monthInfo[$k]['month']-1]['profit'] = $monthInfo[$k]['profit'];
                //拼接本月的数据
            }
            // dump($num);die;
            $res['month'] = $num;
            //本月数据
            $key= count($agentInfo)-1;
            $res['month_teach_profit'] = $agentInfo[$key]['month_teach_profit'] ;
            $res['month_pos_profit'] = $agentInfo[$key]['month_pos_profit']; 
            $res['month_train_profit'] = $agentInfo[$key]['month_train_profit'];
            $res['month_quick_money'] = $agentInfo[$key]['month_quick_money'];
            $res['month_normal_money'] = $agentInfo[$key]['month_normal_money'];
            $res['month_yquick_money'] = $agentInfo[$key]['month_yquick_money'];
            $res['month_quick_profit'] = $agentInfo[$key]['month_quick_profit'];
            $res['month_normal_profit'] = $agentInfo[$key]['month_normal_profit'];
            $res['month_yquick_profit'] = $agentInfo[$key]['month_yquick_profit'];
            if(isset($agentInfo[$key-1]['machine_num'])){
                $res['machine_num'] = $agentInfo[$key]['machine_num'] - $agentInfo[$key-1]['machine_num'];
            }else{
                $res['machine_num'] = $agentInfo[$key]['machine_num'];
            }
        }else{
            $res['month'] = array();
            $res['month_teach_profit'] = '';
            $res['month_pos_profit'] = '';
            $res['month_train_profit'] = '';
            $res['month_quick_money'] = '';
            $res['month_normal_money'] = '';
            $res['month_yquick_money'] = '';
            $res['machine_num'] = '';
            $res['month_quick_profit'] = '';
            $res['month_normal_profit'] = '';
            $res['month_yquick_profit'] = '';
        } 
        return $res;
    } 

    //累计收益
    public function allCoin($user_id)
    {
        $year = date('Y');
        $month = date('m');
        $info = Db::table('user_money')->field('month_teach_profit,month_pos_profit,month_direct_profit,month_train_profit,month,month,all_profit')->order('month desc')->where("user_id='$user_id'")->select()->toArray();
        if($info){
            // $end = $this->infoReturn($info);
            foreach ($info as $k => $v) {
                $monthInfo[$k]['profit'] = $v['month_teach_profit'] + $v['month_pos_profit'] + $v['month_direct_profit'] + $v['month_train_profit'];
                $monthInfo[$k]['month'] = $v['month'];
                $monthInfo[$k]['month'] = substr($v['month'],5,1);
                $monthInfo[$k]['year'] = substr($v['month'],0,4);
                // $k1 = $k-1;
                // if($k1>=0){
                //     if($monthInfo[$k]['year'] = $monthInfo[$k1]['year']){
                //         $a[$k1]['year'] = $monthInfo[$k]['year'];
                //         $a[$k]['month'][$k]['profit'] = $monthInfo[$k]['profit'];
                //         $a[$k]['month'][$k]['month'] = $monthInfo[$k]['month'];
                //         $a[$k]['month'][$k1]['profit'] = $monthInfo[$k1]['profit'];
                //         $a[$k]['month'][$k1]['month'] = $monthInfo[$k1]['month'];                    
                //     }
                // }else{
                //     // dump($info);die;
                //     $a[$k]['year'] = $monthInfo[$k]['year'];
                //     $a[$k]['month'][$k]['profit'] = $monthInfo[$k]['profit'];
                //     $a[$k]['month'][$k]['month'] = $monthInfo[$k]['month'];                
                // }
                // dump($a);die;
            }
            // dump($monthInfo);die;
            $end = $this->infoReturn($monthInfo);
            // dump($monthInfo);die;
            //拼接本月的数据 
            // $res['year'] = $year;
            $res['profit'] = $info[0]['all_profit'];
            $res['month'] = $end;
        }else{
            $res = array();
        }      
        return $res;
    } 

    // 
    public function infoReturn($monthInfo){
        $res = array(); 
        // dump($monthInfo);die;
        foreach ($monthInfo as $k => $v) {
          $res[$v['year']][] = $v;
          // unset($res[$v['year']][$k]['year']);
        }
        $keys = array_keys($res);
        $end = array();
        foreach($keys as $k => $v){
            $end[$k]['month'] = $res[$v];
            $end[$k]['year'] = $v;
        }
        // dump($end);die; 
        return $end;    
    }   
	   

}
